package com.wx.app.ui.activity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.administrator.newemptyapplication.R;
import com.squareup.picasso.Picasso;
import com.wx.app.mylib.app.BaseActivity;
import com.wx.app.mylib.view.ViewPagerFixed;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import uk.co.senab.photoview.PhotoView;

public class PhotoTest extends BaseActivity {

    ArrayList<String> bannerData = new ArrayList<>();
    private MyPagerAdapter myPagerAdapter;
    @BindView(R.id.view_pager)
    ViewPagerFixed viewPager;
    @BindView(R.id.iv_test_ph_close)
    ImageView ivClose;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_test);
        ButterKnife.bind(mActivity);

        initView();

        initData();
    }

    private void initView() {
        myPagerAdapter = new MyPagerAdapter(bannerData);
        viewPager.setAdapter(myPagerAdapter);
    }

    @OnClick(R.id.iv_test_ph_close)
    void close() {
        finish();
    }

    private void initData() {
        String url = "http://7xl2ga.com1.z0.glb.clouddn.com/Banners.txt";
        OkHttpUtils.get()
                .url(url)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                        showToast("请求服务器出错！");
                    }

                    @Override
                    public void onResponse(String response) {
                        JSONObject data = JSONObject.parseObject(response);
                        JSONArray array = data.getJSONArray("bannerData");
                        for (int i = 0; i < array.size(); i++) {
                            JSONObject object = array.getJSONObject(i);
                            String url = object.getString("banner_url");
                            bannerData.add(url);
                        }
                        myPagerAdapter.notifyDataSetChanged();
                    }
                });
    }

    public class MyPagerAdapter extends PagerAdapter {
        ArrayList<String> data;
        private PhotoView photoView;

        public MyPagerAdapter(ArrayList<String> bannerData) {
            data = bannerData;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            photoView = new PhotoView(container.getContext());
            photoView.setScaleType(ImageView.ScaleType.FIT_XY);
            Picasso.with(mActivity).load(bannerData.get(position)).into(photoView);
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            container.removeView(photoView);
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }

}
